package com.lczc.soa.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lczc.soa.domain.Announce;
import com.lczc.soa.domain.SearchAnnounceResponse;
import com.lczc.soa.mapper.AnnounceMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnnounceServiceImpl implements AnnounceService {

    @Autowired
    private AnnounceMapper announceMapper;

    @Override
    public int saveAnnounce(Announce announce) {
        if(StringUtils.isNotBlank(announce.getAnnounceId())){
            return announceMapper.updateByPrimaryKey(announce);
        }else {
            return announceMapper.insert(announce);
        }
    }

    @Override
    public List<Announce> listAnnounce() {
        return announceMapper.selectAll();
    }

    @Override
    public SearchAnnounceResponse findAllAnnounce(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Announce> announces = announceMapper.selectAll();
        return new SearchAnnounceResponse(((Page)announces).getTotal(),announces);
    }

    @Override
    public SearchAnnounceResponse findAnnounceLikeAnnounceName(String keywords, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Announce> announces = announceMapper.selectByLikeAnnounceName(keywords);
        return new SearchAnnounceResponse(((Page)announces).getTotal(),announces);
    }

    @Override
    public int deleteAnnounce(String id) {
        return announceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Announce findAnnounceByPrimaryKey(String id) {
        return announceMapper.selectByPrimaryKey(id);
    }
}
